﻿using System;
using System.Reflection;
using System.Web.Mvc;
using UnionManager.Infrastructure.Config;
using UnionManager.Models;

namespace UnionManager.Controllers
{
    public class BaseController : Controller
    {
        [AttributeUsage(AttributeTargets.Method, AllowMultiple = false, Inherited = true)]
        public class MultipleButtonAttribute : ActionNameSelectorAttribute
        {
            public string Name { get; set; }
            public string Argument { get; set; }

            public override bool IsValidName(ControllerContext controllerContext, string actionName, MethodInfo methodInfo)
            {
                bool isValidName = false;
                string keyValue = string.Format("{0}:{1}", Name, Argument);
                var value = controllerContext.Controller.ValueProvider.GetValue(keyValue);
                if (value != null)
                {
                    value = new ValueProviderResult(Argument, Argument, null);
                    controllerContext.Controller.ControllerContext.RouteData.Values[Name] = Argument;
                    isValidName = true;
                }

                return isValidName;
            }
        }

        //public class AdminLevelAttribute : FilterAttribute, IAuthorizationFilter
        //{
        //    public void OnAuthorization(AuthorizationContext filterContext)
        //    {
        //        var controller = filterContext.Controller as BaseController;
        //        if (controller == null)
        //            return;
        //        if (controller.CurrentUser.MaQuyen != "Admin")
        //            filterContext.Result = controller.RedirectToLogin();
        //    }
        //}

        public TAIKHOAN CurrentUser
        {
            get
            {
                var principal = User as EnhancedPrincipal;
                return principal == null ? null : principal.Data;
            }
        }

        public bool CheckPermission()
        {
            return true;
        }

    }
}
